home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rjs.lha / RJS / CmdLine / src / CmdLine.h < prev    next >
C/C++ Source or Header  |  1991-06-14  |  5KB  |  173 lines

  1. #ifndef RJS_CMDLINE_CLASS_H
  2. #define RJS_CMDLINE_CLASS_H
  3.  
  4. #include<RJS/String.h>
  5.  
  6. class RJS_CmdOpt {
  7.     friend class RJS_CmdLine;
  8. public:
  9.     enum CmdOptFlags { 
  10.         None=0x00,        // no flags
  11.         Value=0x01,         // value needed
  12.         ValueOpt=0x02,         // value is optional 
  13.         Required=0x04,         // this option required!
  14.         Default=0x08,        // has default value
  15.         Parameter=0x10        // parameter, not -flag (not done)
  16.     };
  17.     enum CmdPresFlags { 
  18.         NotPresent=0x00,     // option not specified
  19.         Present=0x01,         // option present
  20.         DefaultValue=0x02,    // default value was taken
  21.         ValuePresent=0x04    // value was present (for ValueOpt)
  22.     };
  23.     RJS_CmdOpt(const char *k, CmdOptFlags f=None);
  24.     RJS_CmdOpt(const char *k, const RJS_String &defval,CmdOptFlags f=None);
  25.     int not_present()    { return present==NotPresent; }
  26.     int is_present()     { return present; }
  27.     int is_default()    { return present & DefaultValue; }
  28.     int value_present()    { return present & ValuePresent; }
  29.  
  30.     int expects_value()    { return flags & Value; }
  31.     int is_required()    { return flags & Required; }
  32.     int value_optional()    { return flags & ValueOpt; }
  33.     int has_default()    { return flags & Default; }
  34.  
  35.     const RJS_String &keyword()        { return key; }
  36.     const RJS_String &value()         { return val; }
  37.     const RJS_String &default_value()     { return dval; }
  38.     virtual const char *value_type();
  39.     int set(const char *s);
  40.     int set_default();
  41.     void set();
  42.     virtual void reset();
  43.     virtual void dump();
  44. protected:
  45.     int virtual set_value();
  46.     RJS_String        key;
  47.     int        abbrev_len;
  48.     CmdOptFlags    flags;
  49.     CmdPresFlags    present;
  50.     RJS_String         val;
  51.     RJS_String         dval;
  52. };
  53.  
  54. class RJS_CmdOpt_flag : public RJS_CmdOpt {
  55. public:
  56.     RJS_CmdOpt_flag(const char *k) : RJS_CmdOpt(k,None) {};
  57. };
  58.  
  59. class RJS_CmdOpt_boolean : public RJS_CmdOpt {
  60. public:
  61.     RJS_CmdOpt_boolean(const char *k, CmdOptFlags f=None);
  62.     RJS_CmdOpt_boolean(const char *k, const RJS_String &dv, CmdOptFlags f=None);
  63.     int value() { return val; }
  64.     const char *value_type();
  65.     virtual void reset();
  66.     virtual void dump();
  67. private:
  68.     int set_value();
  69.     int val;
  70. };
  71.  
  72. class RJS_CmdOpt_double : public RJS_CmdOpt {
  73. public:
  74.     RJS_CmdOpt_double(const char *k, CmdOptFlags f=None);
  75.     RJS_CmdOpt_double(const char *k, const RJS_String &dv,CmdOptFlags f=None);
  76.     double value() { return val;}
  77.     const char *value_type();
  78.     virtual void reset();
  79.     virtual void dump();
  80. private:
  81.     int set_value();
  82.     double val;
  83. };
  84.  
  85. class RJS_CmdOpt_int : public RJS_CmdOpt {
  86. public:
  87.     RJS_CmdOpt_int(const char *k, CmdOptFlags f=None);
  88.     RJS_CmdOpt_int(const char *k, const RJS_String &df, CmdOptFlags f=None);
  89.     int value() { return val; }
  90.     const char *value_type();
  91.     virtual void dump();
  92.     virtual void reset();
  93. private:
  94.     int set_value();
  95.     int val;
  96. };
  97.  
  98. class RJS_CmdOpt_string : public RJS_CmdOpt {
  99. public:
  100.     RJS_CmdOpt_string(const char *k, CmdOptFlags f=None);
  101.     RJS_CmdOpt_string(const char *k, const RJS_String &df, CmdOptFlags f=None);
  102. };
  103.  
  104. class RJS_CmdOpt_int_range : public RJS_CmdOpt {
  105. public:
  106.     RJS_CmdOpt_int_range(const char *k, CmdOptFlags f=None);
  107.     RJS_CmdOpt_int_range(const char *k, const RJS_String &df,CmdOptFlags f=None);
  108.     int low_value() { return lval; }
  109.     int low_is_present() { return l_present; }
  110.     int high_value() { return hval; }
  111.     int high_is_present() { return h_present; }
  112.     const char *value_type();
  113.     void reset();
  114.     virtual void dump();
  115. private:
  116.     int set_value();
  117.     int lval;
  118.     int hval;
  119.     RJS_CmdOpt::CmdPresFlags l_present;
  120.     RJS_CmdOpt::CmdPresFlags h_present;
  121. };
  122.  
  123. class RJS_CmdLineWord {
  124. public:
  125.     RJS_CmdLineWord( char **argv);
  126.     RJS_CmdLineWord( RJS_String &s, int dcl);
  127.     ~RJS_CmdLineWord();
  128.     int operator() (RJS_String &word);
  129.     int peek(RJS_String &word);
  130. private:
  131.     RJS_StringScan *scan;
  132.     char **argv;
  133.     int peek_flag; 
  134.     RJS_String peeked;
  135. };
  136.  
  137. class RJS_CmdLine {
  138. public:
  139.     enum CmdLineFlags { 
  140.         Abort=0x01, 
  141.         Messages=0x02,
  142.         Once=0x04,
  143.         Abbrev=0x08,
  144.         NoCase=0x10,
  145.         DCL=0x20
  146.     };
  147.     enum ParseStatus { Ok, UnknownOption, BadValue, NoValue,MissingRequired,
  148.                MoreThenOnce};
  149.     RJS_CmdLine(RJS_CmdOpt **options, const char *p,
  150.         CmdLineFlags flags=CmdLineFlags(Abort|Messages|Abbrev));
  151.     ParseStatus  parse(char **argv, RJS_String &xtra);
  152.     ParseStatus  parse(RJS_String &cmdline, RJS_String &xtra);
  153.     ParseStatus  parse(RJS_CmdLineWord &clw, RJS_String &xtra);
  154.     void dump();
  155.     only_once() { return flags & Once; }
  156.     abort() { return flags & Abort; }
  157.     messages() { return flags & Messages; }
  158.     dcl () { return flags & DCL; }
  159.     abbrev() { return flags & Abbrev; }
  160.     no_case() { return flags & NoCase; }
  161.     RJS_String &last_opt() { return opt; }
  162.     RJS_String &last_val() { return val; }
  163. private:
  164.     void setup_abbrevs();
  165.       const char *prefix;        // prefix for messages
  166.     CmdLineFlags flags;
  167.     RJS_String opt,val;         // last option worked on 
  168.     RJS_CmdOpt **options;
  169.     char sep;            // seperator '-' for Unix, '/' for DCL
  170. };
  171.  
  172. #endif
  173.